<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="消息标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入消息标题"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人员" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入发布人员"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入发布人员"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >发布消息</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="noticeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="100" />
      <el-table-column
        label="消息标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="发布人员"
        align="center"
        prop="creatorName"
        width="100"
      />
      <el-table-column
        label="发布时间"
        align="center"
        prop="createTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接收对象" align="center" width="100">
        <template #default="scope">
          <span>{{ scope.row.receNum }}人</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="查看" placement="top">
            <el-button
              link
              type="primary"
              icon="View"
              @click="handleDetail(scope.row)"
              v-hasPermi="['system:notice:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:notice:remove']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-row v-show="total > 0">
      <el-col :span="12">
        <el-button
          type="danger"
          plain
          :disabled="ids.length == 0"
          @click="handleBatchDelete"
          v-hasPermi="['system:notice:add']"
          >批量删除</el-button
        >
      </el-col>
      <el-col :span="12">
        <pagination
          :total="total"
          v-model:page="queryParams.page_size"
          v-model:limit="queryParams.pageCount"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加 -->
    <el-dialog :title="title" v-model="open" width="560px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="发送对象" prop="orgIds">
              <el-tree-select
                style="width: 100%"
                v-model="form.orgIds"
                :data="organizationTreeList"
                multiple
                :render-after-expand="false"
                show-checkbox
                value-key="id"
                check-on-click-node
                :props="{
                  label: 'orgName',
                  children: 'childs',
                }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input
                v-model="form.title"
                show-word-limit
                maxlength="20"
                placeholder="请输入标题"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <el-input
                :rows="6"
                maxlength="500"
                show-word-limit
                type="textarea"
                placeholder="请输入内容"
                v-model="form.content"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog
      title="查看消息"
      class="scrollbar"
      v-model="detailVisible"
      width="560px"
      append-to-body
    >
      <div class="dialog-content">
        <h2 class="text-center">{{ form.title }}</h2>
        <p class="text-center">{{ form.createTime }}</p>
        <el-divider />
        <div class="padding-content">
          {{ form.content }}
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="WebMessage">
import {
  list,
  add,
  detail,
  del,
  batchDel,
  getOrganization,
} from "@/api/business/web-message";

const { proxy } = getCurrentInstance();
const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const title = ref("");
const detailVisible = ref(false);
const organizationTreeList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageCount: 10,
    pageSize: 1,
    title: undefined,
    createBy: undefined,
    status: undefined,
  },
  rules: {
    orgIds: [{ required: true, message: "发送对象不能为空", trigger: "blur" }],
    title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
    content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

// 统计发送对象数量
watch(
  () => data.form.orgIds,
  (newValue, oldValue) => {
    console.log(newValue);
    data.form.receNum = newValue.length;
  }
);
/** 查询消息列表 */
function getList() {
  loading.value = true;
  list(queryParams.value).then((response) => {
    noticeList.value = response.data.list;
    total.value = response.data.total_page;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    title: "",
    orgIds: [],
    content: "",
    receNum: "0",
  };
  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageCount = 10;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  getOrganization().then((res) => {
    organizationTreeList.value = res;
    open.value = true;
    title.value = "发送站内信";
  });
}
/**详情 */
function handleDetail(row) {
  const ids = row.id || ids.value;
  detail(ids).then((response) => {
    form.value = response.data;
    detailVisible.value = true;
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      add(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id;
  proxy.$modal
    .confirm('是否确认删除消息编号为"' + ids + '"的数据项？')
    .then(function () {
      return del(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 批量删除 */
function handleBatchDelete() {
  const data = ids.value;
  proxy.$modal
    .confirm('是否确认删除消息编号为"' + data.join(",") + '"的数据项？')
    .then(function () {
      return batchDel({ ids: data });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
getList();
</script>
